<template>
  <div class="home">
    <body id="poster">
      <h1>后台管理页面</h1>
      <el-input
        v-model="query.name"
        style="width: 200px"
        placeholder="请输入姓名"
      ></el-input>
      <el-input
        v-model="query.city"
        style="width: 200px"
        placeholder="请输入城市"
      ></el-input>
      <el-input
        v-model="query.phone"
        style="width: 200px"
        placeholder="请输入电话"
      ></el-input
      >&nbsp;
      <el-button type="success" style="width: 80px" plain @click="getList()"
        >查询</el-button
      >
      <el-button
        type="success"
        style="width: 80px"
        plain
        @click="handleInsert()"
        >添加</el-button
      >
      <el-table
        :data="tableData"
        :header-cell-style="{ 'text-align': 'center' }"
        :cell-style="{ 'text-align': 'center' }"
        style="width: 100%"
      >
        <el-table-column prop="name" label="姓名" width="180"></el-table-column>
        <el-table-column prop="age" label="年龄"></el-table-column>
        <el-table-column prop="sex" label="性别"></el-table-column>
        <el-table-column prop="phone" label="电话"></el-table-column>
        <el-table-column prop="city" label="地址"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button size="mini" type="danger" @click="handleDelete(scope)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="this.query.page"
        :page-sizes="[6, 12, 18, 24]"
        :page-size="this.query.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.total"
      >
      </el-pagination>
      <el-dialog title="用户信息" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item label="姓名">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="年龄">
            <el-input v-model="form.age" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="性别">
            <el-input v-model="form.sex"></el-input>
          </el-form-item>
          <el-form-item label="电话">
            <el-input v-model="form.phone" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="城市">
            <el-input v-model="form.city" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitForm()">确 定</el-button>
        </div>
      </el-dialog>
    </body>
  </div>
</template>

<<script>
export default {
  name:'StartS',
  data() {
    return {
      tableData: [],
      total: 0, //自定total
      query: {
        //这两个字段最好和后端一致
        page: 1,
        size: 5,
        //下面三个字段是条件查询的值
        name: '',
        city: '',
        phone: ''
      },
      dialogFormVisible: false,
      form: {
        id: '',
        name: '',
        age: '',
        phone: '',
        sex: '',
        city: '',
      },
    }
  },

  mounted() {
    //页面加载时调用此方法
    this.getList();
  },
  methods: {
    getList() {
      this.axios.get("http://39.106.137.34:8083/user/getList", {
        params: {
          //前端传值
          page: this.query.page,
          size: this.query.size,
          name: this.query.name,
          city: this.query.city,
          phone: this.query.phone
        }
      }).then((response) => {
        //遍历复制
        this.tableData = response.data.content.list;
        this.total = response.data.content.total;
      })
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      //点击重置每页条数，然后重新查询
      this.query.size = val;
      this.getList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      //点击更新页码，重新查询
      this.query.page = val;
      this.getList();

    },
    handleDelete(scope) {
      //scope是行内的对象,拿到row里对象的id
      console.log(scope.row.id)
      let id = scope.row.id;
      this.axios.delete("http://39.106.137.34:8083/user/delete/" + id)
          .then(resp => {
            let data = resp.data;
            if (data.success) {
              this.getList();
             this.$confirm('是否确认删除?', '提示', {
               confirmButtonText: '确定',
               cancelButtonText: '取消',
                type: 'warning'
        })
            }
          
          })
    },
    handleInsert() {
      this.form = {};
      this.dialogFormVisible = true;
    },
    //修改-----------------------------------------------
    handleEdit(row) {
      //表单对话框赋值
      this.form = row;
      //把表单对话框显示
      this.dialogFormVisible = true;
    },
    submitForm() {
      this.axios.post("http://39.106.137.34:8083/user/save/", this.form)
          .then(resp => {
            let info = resp.data;
            if (info.success) {
              this.dialogFormVisible = false;
              this.from = {};
              this.getList();
              this.$message({
                message: '操作成功',
                type: 'success'
              });
            }
          })
    }
  }
}
</script>

<style scoped>
#poster {
  background-position: center;
  height: 100%;
  width: 100%;
  background-size: cover;
  position: fixed;
}
body {
  margin: 0px;
  padding: 0px;
  /* background-color:rgb(156, 185, 216); */
  background-image: url("../assets/back.jpg");
}
</style>
